#!/usr/bin/env python
# encoding: utf-8


"""
@file: zuizhiqiucan.py
@time: 2017/4/25 下午2:49
"""
# 三角函数最值点 求参
from mathsolver.functions.base import *
from sympy import sin, cos, pi
from mathsolver.functions.sanjiao import sanjiao_utils as su
from itertools import chain
from mathsolver.functions.sanjiao.huajian import trig_simplify


# 给定一个函数 在某一处取得最值 得出一个BaseEqs
# style1 Input: paramer1:三角函数; paramer2:最值位; paramer3:最值类型; paramer4:最值 Ouput: 参数之间的关系
class SanJiaoHanShuZuiZhiChuCondition(BaseFunction):
    def solver(self, *args):
        self.label.add('利用三角函数图像特殊点求参数')
        arg1 = args[0]
        arg2 = args[1]
        _x, x0 = arg2.sympify()
        arg3 = args[2]
        arg4 = args[3]
        op_text = arg3
        op_value = arg4.sympify()
        op_type = 'max' if op_text.find('最大值') >= 0 else 'min'
        f_symbol, f_expr = arg1.sympify()
        f_sim_expr = trig_simplify(f_expr)  # 化简
        if f_sim_expr != f_expr:
            self.steps.append([arg1.printing(), ' = ' + new_latex(f_sim_expr)])
        x_symbol = sympify('x')
        _k = sympify('k')
        _2kpi = 2 * _k * pi
        _A, t_type, t_arg, _b = su.simp_trig_info(f_sim_expr)
        _w = t_arg.coeff(x_symbol)
        _h = t_arg.subs(x_symbol, 0)
        if t_type == sin:
            arg1 = t_arg.subs(x_symbol, x0)
            if op_type == 'max':
                eq1 = BaseEq([arg1, _2kpi + pi / 2])
                eq2 = BaseEq([_A + _b, op_value])
                self.steps.append(['由题意可知:', ''])
                self.steps.append(['' + eq1.printing(), ' ' + su.print_belong_z(_k)])
                self.steps.append(['' + eq2.printing(), ' '])
            else:
                eq1 = BaseEq([arg1, _2kpi + 3 * pi / 2])
                eq2 = BaseEq([-_A + _b, op_value])
                self.steps.append(['由题意可知:', ''])
                self.steps.append(['' + eq1.printing(), ' ' + su.print_belong_z(_k)])
                self.steps.append(['' + eq2.printing(), ' '])
        elif t_type == cos:
            arg1 = t_arg.subs(x_symbol, x0)
            if op_type == 'max':
                eq1 = BaseEq([arg1, _2kpi])
                eq2 = BaseEq([_A + _b, op_value])
                self.steps.append(['由题意可知:', ''])
                self.steps.append(['' + eq1.printing(), ' ' + su.print_belong_z(_k)])
                self.steps.append(['' + eq2.printing(), ' '])
            else:
                eq1 = BaseEq([arg1, _2kpi + pi])
                eq2 = BaseEq([-_A + _b, op_value])
                self.steps.append(['由题意可知:', ''])
                self.steps.append(['' + eq1.printing(), ' ' + su.print_belong_z(_k)])
                self.steps.append(['' + eq2.printing(), ' '])
        else:
            raise Exception('Type Match Error')
        eqs = chain(eq1.sympify(), eq2.sympify())
        self.output.append(BaseEqs(eqs))
        return self


if __name__ == '__main__':
    pass
